package com.zmj.tl.database.taos.controller;

import com.zmj.tl.database.taos.domain.TreatMetric;
import com.zmj.tl.database.taos.domain.TreatSchema;
import com.zmj.tl.database.taos.service.TreatService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author hd
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/treat")
public class TreatController {
    private final TreatService treatService;

    @PostMapping(value = "/createDb")
    public ResponseEntity<Integer> createDatabase(@RequestBody TreatSchema treatSchema) {
        return ResponseEntity.ok(this.treatService.createDatabase(treatSchema));
    }

    @PostMapping(value = "/createTb")
    public ResponseEntity<Integer> createTable(@RequestBody TreatSchema treatSchema) {
        return ResponseEntity.ok(this.treatService.createTable(treatSchema));
    }

    @PostMapping(value = "/createStb")
    public ResponseEntity<Integer> createStable(@RequestBody TreatSchema treatSchema) {
        return ResponseEntity.ok(this.treatService.createStable(treatSchema));
    }

    @PostMapping(value = "/dropTb")
    public void dropTable(@RequestBody TreatSchema treatSchema) {
        this.treatService.dropTable(treatSchema);
    }

    @PostMapping(value = "/dropDb")
    public void dropDatabase(@RequestBody TreatSchema treatSchema) {
        this.treatService.dropDatabase(treatSchema);
    }

    @PostMapping(value = "/addOne")
    public void addOneMeter(@RequestBody TreatMetric resource) {
        resource.setTs(new Date());
        this.treatService.addOne(resource);
    }

    @PostMapping(value = "/addEntity")
    public void addMeterEntity(@RequestBody TreatMetric resource) {
        resource.setTs(new Date());
        this.treatService.addEntity(resource);
    }

    @PostMapping(value = "/addBatch")
    public void addMeters(@RequestBody TreatMetric resource) {
        for (TreatMetric treatMetric : resource.getList()) {
            treatMetric.setTs(new Date());
        }
        this.treatService.addBatch(resource);
    }
}
